var map;
var markers = [];
var latLngs = [];
var windows = [];
var myLatlng;
var ofertaObj;
var relevanciaHTML = '';
var infowindowHTML = '';
var flag = 0;
var marcadorAberto = null;

var getRelevancia = new Request({
    url : 'include/request/getRelevancia.php',
    onSuccess: function(response){
	relevanciaHTML = response;
    },
    async:false
});

var getInfowindow = new Request({
    url : 'include/request/getInfowindow.php',
    onSuccess: function(response){
	infowindowHTML = response;
    },
    async:false
});

var addLido = new Request({
    url : 'include/request/addLido.php',
    async:false
});

function setLinks(obj){
    var addRelevancia = new Request.HTML({
	url : 'include/request/addRelevancia.php',
	update: $('relevancia'+obj.id_mapa)
    });
    var deleteRelevancia = new Request.HTML({
	url : 'include/request/deleteRelevancia.php',
	update: $('relevancia'+obj.id_mapa)
    });
    if(obj.opt>1)
	deleteRelevancia.post(obj)
    else
	addRelevancia.post(obj)


}


function initialize() {
  var myLatlng = new google.maps.LatLng(-22.950944078667128,  -43.18862902539058);
   var myOptions = {
	zoom: 13,
	center: myLatlng,
	mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

//			  google.maps.event.addListener(map, 'click', function(event) {
//				placeMarker(event.latLng);
//			  });
}

function placeMarkerRead(location , index) {

    var marker = new google.maps.Marker({
	position    : location.latLng,
	map	    : map,
	icon	    : location.icone,
	id	    : location.id,
	id_usuario  : location.id_usuario,
	lido	    : location.lido,
	location    : location,
	infowindow  : null
    });	   

    google.maps.event.addListener(marker, 'click', function() {
	
	if(marcadorAberto != null){
	    markers[marcadorAberto].infowindow.setMap(null);
	}
	
	
	getRelevancia.post({id_mapa:marker.id});
	location.relev = relevanciaHTML;

	getInfowindow.post({
	    relev	: relevanciaHTML,
	    latLng	: location.latLng,
	    conteudo	: location.conteudo,
	    icone	: location.icone,
	    dtPost	: location.dtPost,
	    titulo	: location.titulo,
	    id		: location.id,
	    id_usuario	: location.id_usuario,
	    nome	: location.nome,
	    dataFim	: location.dataFim,
	    fotos	: location.fotos,
	    minhaPosicao: index
	    
	});

//	var infowindow = new google.maps.InfoWindow({
//	    content: infowindowHTML
//	});

	var infoBox = new InfoBox({latlng: marker.getPosition(), map: map, conteudo:infowindowHTML});
	relevanciaHTML = '';
//	infowindow.open(map,marker);
	
	marker.infowindow = infoBox;
    });

    markers.push(marker);
}

function teste(){
	return markers;
}

function deleteOverlays() {
      if (markers) {
	    for (i in markers) {
		    markers[i].setMap(null);
	    }
	    markers.length = 0;
      }
}

function printingMarkers(arrMarkers) {
    for (i = 0; i < arrMarkers.length; i++) {
	   placeMarkerRead(arrMarkers[i] , i);
    }
}

var marker = new google.maps.Marker({
    position:myLatlng,
    map: map
});

//google.maps.event.addListener(marker, 'click', function() {
//  infowindow.open(map,marker);
//});


window.addEvent('domready', function(){

	var save = new Request({
		url: 'include/request/save.php'
	});

	$$('.save').addEvent('click',function(){
			var m = teste();
			save.post({markers:m});
	});

	$$('.read').addEvent('click',function(){
			read.send();
	});

	$$('.deleteAll').addEvent('click',function(){
			deleteOverlays();
	});
});

var readBusca = new Request.JSON({
	url: 'include/request/readBuscaGeral.php',
	async: false,
	onSuccess: function(responseJSON ){

		var coordenadas = responseJSON;

		var lat,lng,conteudo,icone, dtPost,titulo,id,id_usuario,nome,dataFim,fotos;
		latLng = null;
		var ofertas = [];

		for (i = 0; i < coordenadas.length; i++) {

			lat	    = coordenadas[i].lat;
			lng	    = coordenadas[i].lng;
			conteudo    = coordenadas[i].content;
			icone	    = coordenadas[i].icon;
			dtPost	    = coordenadas[i].dtPost;
			titulo	    = coordenadas[i].titulo;
			id	    = coordenadas[i].id;
			id_usuario  = coordenadas[i].id_usuario;
			nome	    = coordenadas[i].nome;
			fotos	    = coordenadas[i].fotos;
			

			latLng	    = new google.maps.LatLng(lat,lng);
			dataFim	    = coordenadas[i].dataFim;

			ofertaObj   = {
			    latLng	: latLng,
			    conteudo	: conteudo,
			    icone	: icone,
			    dtPost	: dtPost,
			    titulo	: titulo,
			    id		: id,
			    id_usuario	: id_usuario,
			    nome	: nome,
			    dataFim	: dataFim,
			    fotos	: fotos
			    
			};

			ofertas.include(ofertaObj);
		}
		
		if(latLng != null)
		    map.setCenter(latLng);
		printingMarkers(ofertas);


	}
});

function readBusca1(){
    readBusca.send();
}

Number.prototype.mod = function(n) {
    return ((this%n)+n)%n;
}

function mostrarMarcador(anterior, i){
    
    if(anterior < i)
	subindo = true;
    else
	subindo = false;
    
    anterior = anterior.mod(markers.length);
    i = i.mod(markers.length);
    
    if(markers[anterior].infowindow != null)
	markers[anterior].infowindow.setMap(null);  
    
    if(markers[i].map == null){	
	
	if(subindo)
	    proximo = (i+1).mod(markers.length);
	else
	    proximo = (i-1).mod(markers.length);
	
	mostrarMarcador(i, proximo);
	return false;
    }
    
    var marker =  markers[i];
    var location = marker.location;
       
    
    addLido.post({id_marcador:marker.id});
    marker.lido = 1;

    getRelevancia.post({id_mapa:marker.id});
    location.relev = relevanciaHTML;
    
    getInfowindow.post({
	relev	: relevanciaHTML,
	latLng	: location.latLng,
	conteudo	: location.conteudo,
	icone	: location.icone,
	dtPost	: location.dtPost,
	titulo	: location.titulo,
	id		: location.id,
	id_usuario	: location.id_usuario,
	nome	: location.nome,
	dataFim	: location.dataFim,
	mostrarSeguir : location.mostrarSeguir,
	minhaPosicao : i,
	qtdMarkers : markers.length,
	fotos	: location.fotos      
    });
    
//    var infowindow = new google.maps.InfoWindow({
//	content: infowindowHTML
//    });

    marcadorAberto = i;
    
    var infoBox = new InfoBox({latlng: marker.getPosition(), map: map, conteudo:infowindowHTML});

    markers[i].infowindow = infoBox;

//    infowindow.open(map, marker);
}